﻿using BusinessDB;
using BusinessResource;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace TaskScheduler
{
    class Program
    {
        private static IList<string> _keys;

        static Program()
        {
            _keys = new List<string>()
            {
                "Cache",
                "Database",
                "TemplateRoot",
                "ImgRoot",
                "ImgUrl",
                "ExportRoot",
                "ExportUrl"
            };
        }

        static void Main(string[] args)
        {
            ReadConfig();
            InsertResource();
            InsertSystemUser();
            Console.ReadLine();
        }

        private static void ReadConfig()
        {
            string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ServerConfig.xml");
            XDocument doc = XDocument.Load(configPath);
            XElement root = doc.Root;

            ServerConfig.Singleton.Database = root.Element("Database").Value;
            ServerConfig.Singleton.CacheUrl = root.Element("Cache").Value;
            ServerConfig.Singleton.TemplateRoot = root.Element("TemplateRoot").Value;
            ServerConfig.Singleton.ImgRoot = root.Element("ImgRoot").Value;
            ServerConfig.Singleton.ImgUrl = root.Element("ImgUrl").Value;
            ServerConfig.Singleton.ExportRoot = root.Element("ExportRoot").Value;
            ServerConfig.Singleton.ExportUrl = root.Element("ExportUrl").Value;
        }

        private static void InsertSystemUser()
        {
            UserService us = new UserService();
            if (us.Exists())
                return;

            us.Add(new UserDB() { Name = "system", Pwd = "zc" });
        }

        private static void InsertResource()
        {
            ResourceService rs = new ResourceService();
            if (rs.Exists())
                return;

            ResItemFactory rf = new ResItemFactory();
            foreach (ResItemDB item in rf.Create())
            {
                rs.Add(item);
            }
        }
    }
}
